package com.myframe.www.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toolbar;

import com.myframe.www.R;
import com.yong.android.framework.view.BaseToolbar;

import butterknife.ButterKnife;


/**
 * Description:
 * Created by yong on 11/29/2017.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private LinearLayout parentLinearLayout;
    protected BaseToolbar mToolbar;
    @SuppressLint("ResourceType")

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreateViewBefore(savedInstanceState);
        initContentView(R.layout.activity_base);
        if (getContentViewLayoutId() < 1) {
            throw new IllegalStateException("activity content ID not use");
        }
        setContentView(getContentViewLayoutId());
        bindView();
        initToolbar();
        onViewCreated(savedInstanceState);
        onCreateViewCompleted();
        ButterKnife.bind(this);
    }

    //隐藏toolbar
    protected void hideToolbar(){
        mToolbar.setVisibility(View.GONE);
    }

    protected abstract void initToolbar();

    protected void initContentView(@LayoutRes int layoutResID){

        ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
        viewGroup.removeAllViews();
        parentLinearLayout = new LinearLayout(this);
        parentLinearLayout.setOrientation(LinearLayout.VERTICAL);
        viewGroup.addView(parentLinearLayout);
        //  add the layout of BaseActivity in parentLinearLayout
        LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
    }

    @LayoutRes
    protected abstract int getContentViewLayoutId();

    /**
     * 在setContentView之后
     */
    protected abstract void onViewCreated(Bundle savedInstanceState);


    protected void bindView() {
        mToolbar = (BaseToolbar) findViewById(R.id.mToolbar);
    }
    /**
     * 在setContentView之前
     *
     * @param savedInstanceState
     */
    protected void onCreateViewBefore(Bundle savedInstanceState) {
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        //  added the sub-activity layout id in parentLinearLayout
        LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
    }

    /**
     * 在onViewCreate之后
     */
    protected void onCreateViewCompleted() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

}
